package pure.ui.groups 
{
	import pure.ui.core.Component;
	import pure.ui.skins.ClickableSkin;
	import pure.ui.skins.IClickSkin;
	import pure.ui.skins.ISkin;
	import pure.ui.skins.Skin;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import pure.ui.ListItem;
	
	/**
	 * 列表
	 * @author Des
	 * <皮肤种类> ---- IClickSkin
	 * 
	 */
	
public class List extends GroupBase
{
	public function List(x:Number, y:Number, skin:ISkin = null, selectable:Boolean = true, infinite:Boolean = false)
	{
		super(x, y, skin);
		_selectable = selectable;
		_infinite = infinite;
	}
	
	

	/*model*/

	protected var _selectable:Boolean;	// 是否可选
	
	protected var _infinite:Boolean	// 是否为无限数目item
	
	/*cache*/
	
	protected var ____changeObserver:Function;	// 选中项目改变回调方法

	protected var ____clickObserver:Function;	// 点击回调方法
	
	protected var _numItemCol:int = 1;	// 无限数目item时的每行item数目
	
	protected var _gapItems:Point; // 无限数目item时的行列项目间距
		
	

	/**
	 * 观察点击状态
	 * @param observer		回调方法
	 **/
	public function setObserverClick(observer:Function):void
	{
		____clickObserver = observer;
		var item:ListItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			item.setObserverClick(observer);
		}
	}
	
	/**
	 * 观察选中项目改变
	 * @param observer		回调方法
	 **/
	public function setObserverChange(observer:Function):void
	{
		____changeObserver = observer;
	}
	
	/**
	 * 添加Item
	 * @param id
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param skin
	 * @param hasLabel
	 **/
	override public function addItem(id:String, x:Number , y:Number, itemSize:Point = null, skin:ISkin = null, hasLabel:Boolean = true):void
	{
		super.addItem(id, x, y, itemSize, skin, hasLabel);
		if (_infinite)
		{
			_updateItemsPosition();
		}
	}
	
	/**
	 * 添加多Item
	 * @param itemsInfo
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param layoutType
	 * @param layoutData
	 * @param hasLabel
	 **/
	override public function addItems(itemsInfo:Object,x:Number,y:Number,itemSize:Point = null,layoutType:int = 2,layoutData:Object = 4,hasLabel:Boolean=true):void
	{
		super.addItems(itemsInfo, x,y,itemSize, layoutType, layoutData, hasLabel);
		if (_infinite)
		{
			_updateItemsPosition();
		}
	}

	/**
	 * 删除item
	 * @param item		列表项目
	 **/
	public function removeItem(item:ListItem):void
	{
		var index:int = _items.indexOf(item);
		if (index == -1)
		{
			throw new Error('List::remove - 不存在的列表项目！');
		}
		else
		{
			item.destroy();
			_items.splice(index, 1);
		}
	}
	
	/**
	 * 删除item通过id
	 * @param id		列表项目id
	 **/
	public function removeItemById(id:String):void
	{
		var item:ListItem = getItemAt(id) as ListItem;
		if (!item)
		{
			throw new Error('List::removeById - 不存在的列表项目 - < '+id+' >！');
		}
		else
		{
			removeItem(item);
		}
	}
	

	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/

	

	
	
	// 皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value is IClickSkin)
		{
			throw new Error('List::skin - 皮肤类型错误！');
		}
		super.skin = value;
	}	
	
	// 被选中id
	override public function set selectedId(value:String):void
	{
		this._selectedId = value;
		if (____changeObserver != null)
		{
			____changeObserver();
		}
	}

	// 是否可选
	public function set selectable(b:Boolean):void
	{
		if (_selectable == b)
		{
			return;
		}
		var item:ListItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			item.selectable = b;
		}
	}
	
	public function get selectable():Boolean
	{
		return _selectable;
	}
	
	// 被选中项目
	public function get selection():ListItem
	{
		var item:ListItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			item = _items[i];
			if (item.id == selectedId)
			{
				return item;
			}
		}
		return null;
	}

	// 无限数目item时的每行item数目(仅infinite为true时有效！)
	public function set numItemCol(value:int):void
	{
		_numItemCol = value;
	}
	
	public function get numItemCol():int
	{
		return _numItemCol;
	}
	
	// 无限数目item时的行列项目间距(仅infinite为true时有效！)
	public function set gapItems(value:Point):void
	{
		_gapItems = value;
	}
	
	public function get gapItems():Point
	{
		return _gapItems;
	}

	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
		
	
	
	
	/**
	 * 调整全部item位置
	 **/
	protected function _updateItemsPosition():void
	{
		var startX:Number = _items[0].width * 0.5;
		var startY:Number = _items[0].height * 0.5;
		var item:ListItem;
		for (var i:int = 0; i < _numItem; i++)
		{
			var col:int = i % _numItemCol;
			var row:int = int(i / _numItemCol);
			item = _items[i];
			item.move(startX + _gapItems.x * col, startY + _gapItems.y * row);
		}
	}
	
	/**
	 * 生成组件项目
	 * @param htmlLabel
	 * @param x
	 * @param y
	 * @param itemSize
	 * @param skin
	 **/
	override protected function _createItem(x:Number,y:Number,itemSize:Point,skin:ISkin,hasLabel:Boolean):IGroupItem
	{
		var item:ListItem = new ListItem(x, y, itemSize, skin, hasLabel);
		item.selectable = this._selectable;
		if (____clickObserver != null)
		{
			item.setObserverClick(____clickObserver);
		}
		item.group = this;
		return item;
	}
	
	
}

}